package com.hyperf.spring.boot.autoconfigure.lock;

import com.hyperf.core.lock.executor.LockExecutor;
import com.hyperf.core.lock.executor.RedissonLockExecutor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lizhaoyang
 */
@Configuration
@ConditionalOnClass({Redisson.class, RedissonClient.class})
@ConditionalOnProperty(prefix = "hyperf.lock", name = "enabled", havingValue = "true", matchIfMissing = true)
public class RedissonLockExecutorAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(LockExecutor.class)
    @ConditionalOnProperty(prefix = "hyperf.lock", name = "lock-type", havingValue = "redisson")
    public LockExecutor lockExecutor(RedissonClient redissonClient) {
        return new RedissonLockExecutor(redissonClient);
    }
}
